package edu.fzu.fundmgr.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.fzu.fundmgr.dao.ClientDao;
import edu.fzu.fundmgr.dao.impl.ClientDaoJDBCImpl;
import edu.fzu.fundmgr.dao.impl.FinancialActDaoJDBCImpl;
import edu.fzu.fundmgr.domain.Client;
import edu.fzu.fundmgr.domain.FinancialAct;
import edu.fzu.fundmgr.domain.Fund;
import edu.fzu.fundmgr.domain.FundHolding;
import edu.fzu.fundmgr.domain.FundTransInfo;
import edu.fzu.fundmgr.service.ClientService;
import edu.fzu.fundmgr.service.FinancialActService;
import edu.fzu.fundmgr.service.FundService;
import edu.fzu.fundmgr.service.impl.ClientServiceImpl;
import edu.fzu.fundmgr.service.impl.FinancialActServiceImpl;
import edu.fzu.fundmgr.service.impl.FundServiceImpl;

public class FinancialTransServlet extends HttpServlet {

	public void loadAll(HttpServletRequest request, HttpServletResponse response) {
		FundService fundService = new FundServiceImpl();
		List<Fund> fundList = fundService.loadAll();

		request.setAttribute("funds", fundList);
		back(request,response,"buy_funds.jsp");
	}
	
	public void loadFundHolding(HttpServletRequest request, HttpServletResponse response) {
		FundService fundService = new FundServiceImpl();
		List<FundHolding> fundHoldingList = fundService.loadFundHolding();

		request.setAttribute("fund_holding", fundHoldingList);
		back(request,response,"fund_holding.jsp");
	}
	
	public void loadFundTrans(HttpServletRequest request, HttpServletResponse response) {
		FundService fundService = new FundServiceImpl();
		List<FundTransInfo> fundTransInfoList = fundService.loadFundTrans();

		request.setAttribute("fund_trans_infos", fundTransInfoList);
		back(request,response,"fund_trans_info.jsp");
	}

	public void back(HttpServletRequest request, HttpServletResponse response,String page){
		try {
			request.getRequestDispatcher(page).forward(request,
					response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String act = request.getParameter("act");
		String result;
		if (act.equals("buy")) {
			loadAll(request, response);
		}else if(act.equals("redom")){
			
		}else if(act.equals("trans_info")){
			loadFundTrans(request, response);
		}else if(act.equals("holding")){
			loadFundHolding(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
